home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: Very Newbie question
- Date: 6 Mar 1996 10:19:37 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4hjorp$2dj@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- bantolov@bantolov.seanet.com (Bruce Antolovich) in
- <bantolov-0603960018080001@bantolov.seanet.com> writes:
-
- >I am very new to programming in C but have a large background in FORTRAN.
- >(please no snickers) ...
-
- There's no reason for snickers. Fortran is still the right answer for
- many problems (so don't abandon it).
-
- >...I am having a problem with the memory location of
- >variables as described by pointers. I define several variables and try to
- >get their address in memory. Unfortunately, my code gives me the same
- >address for all variables! Any clues as to where I'm going wrong would be
- >very appreciated.
-
- The changes to your printf statements in the slight revision of your
- code should answer your questions. In it, as usual, my comments are of
- the form `/* mha - ... */'.
-
- >I don't think that it matters but I'm using Metrowerks CW on a PowerMac 6100/60
-
- Thanks for the info, but you are right: this is a pure C problem.
-
- #include <stdio.h>
-
- /***********************/
- /* Function Prototypes */
- /***********************/
- void SquareIt(int number, int *squarePtr, int *cubePtr);
-
- int main(void)
- {
- int square;
- int cube;
- int *myCube;
- int *mySquare;
- double test;
- mySquare = □
- myCube = &cube;
- printf("%p \n", (void *) &test); /* mha - added (void *) and
- * changed "%d" (int format) to
- * "%p" (pointer format) here
- * and in the next two
- * printf()s and the 1st two in
- * SquareIt(). There is no
- * guaranteed relationship
- * between an int and a
- * pointer, and the pointers
- * should be cast to (void *)
- * since that is what "%p"
- * expects and, because
- * printf() is a variadic
- * function, there is no
- * conversion implied for the
- * pointers by the prototype */
- printf("The address of the variable square is %p. \n", (void *) mySquare);
- printf("The address of the variable cube is %p. \n", (void *) myCube);
- SquareIt(5, &square, &cube);
- printf("5 squared is %d.\n", square);
- printf("5 cubed is %d. \n", cube);
- return 0;
- }
-
- void SquareIt(int number, int *squarePtr, int *cubePtr)
- {
- printf("squarePtr is %p. (the address of the calling variable)\n ",
- (void *) squarePtr); /* mha - see the comments above for
- * those printf()s */
- printf("cubePtr is %p. (the address of the calling variable) \n",
- (void *) cubePtr);
-
- *squarePtr = number * number;
- printf("%d \n", *squarePtr);
- *cubePtr = number * (*squarePtr);
- }
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-